package com.baizhi.app.appservice.impl;

import com.baizhi.app.appservice.CmfzArticleAppService;
import com.baizhi.entity.CmfzArticle;
import com.baizhi.entity.CmfzUser;
import com.baizhi.mapper.CmfzArticleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Service
public class CmfzArticleAppServiceImpl implements CmfzArticleAppService {
    @Autowired
    private CmfzArticleMapper cmfzArticleMapper;
    @Override
    public List<CmfzArticle> selectAnyArticle(String type) {
        /*根据type类型获取最新的上师文章*/
        if (type.equals("new")){
            List<CmfzArticle> cmfzArticles = cmfzArticleMapper.selectNewArticle();
            return cmfzArticles;
            /*获取全部的上师文章*/
        }else if (type.equals("all")){
            List<CmfzArticle> cmfzArticles = cmfzArticleMapper.selectList(null);
            return cmfzArticles;
            /*获取用户关注的上师的全部文章*/
        }else {
            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = requestAttributes.getRequest();
            CmfzUser user = (CmfzUser) request.getSession().getAttribute("user");
            if (user==null){
                return null;
            }else {
                List<CmfzArticle> cmfzArticles = cmfzArticleMapper.selectArticleByGuruId(user.getGuruId());
                return cmfzArticles;
            }
        }
    }
}
